package com.chenqi.community.controller;

import com.chenqi.community.entity.Post;
import com.chenqi.community.entity.User;
import com.chenqi.community.service.PostService;
import com.chenqi.community.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;

/**
 * @author Ardai
 */
@Controller
//@RequestMapping("/community")
public class AdminController {

    @Autowired
    private PostService postService;

    @Autowired
    private UserService userService;

    /**
     * 后台获取所有帖子数据
     * @param modelAndView
     * @return
     */

    @GetMapping("/admFindPost")
    public ModelAndView admFindPost(ModelAndView modelAndView) {
        List<Post> posts = postService.findAllPost();
        modelAndView.addObject("posts", posts);
        modelAndView.setViewName("/admin/post_list");
        return modelAndView;
    }

    /**
     * 删除帖子 同时删除对应的帖子附图
     * @param pid
     * @param modelAndView
     * @return
     * @throws FileNotFoundException
     */

    @GetMapping("/admDeletePost")
    public ModelAndView admDeletePost(Integer pid,ModelAndView modelAndView) throws FileNotFoundException {
        //删除帖子的同时,删除对应的附图
        Post post = postService.findPostById(pid);
        String realPath = ResourceUtils.getURL("classpath:").getPath() + "static";
        String dataPath = post.getPostImg();
        String delPath = realPath + dataPath;
        File file = new File(delPath);
        System.out.println(file.exists());
        if (file.exists())
        file.delete();
        postService.removePost(pid);
        modelAndView.setViewName("redirect:/admFindPost");
        return modelAndView;
    }

    /**
     * 查找所有用户
     * @param modelAndView
     * @return
     */

    @GetMapping("/admFindAllUser")
    public ModelAndView admFindAllUser(ModelAndView modelAndView) {
        List<User> users = userService.findAllUser();
        modelAndView.addObject("users", users);
        modelAndView.setViewName("/admin/user_list");
        return modelAndView;
    }

    /**
     * 锁定用户和解锁
     * @param uid
     * @param MyStatus
     * @param modelAndView
     * @return
     */
    @GetMapping("/admLockUser")
    public ModelAndView admLockUser(Integer uid,Integer MyStatus,ModelAndView modelAndView) {
        userService.LockAndUnlock(uid,MyStatus);
        modelAndView.setViewName("redirect:/admFindAllUser");
        return modelAndView;
    }

    /**
     * 修改用户权限
     * @param uid
     * @param owner
     * @param modelAndView
     * @return
     */

    @GetMapping("/admEditOwner")
    public ModelAndView admEditOwner(Integer uid,Integer owner,ModelAndView modelAndView) {
        userService.editUserOwner(uid, owner);
        modelAndView.setViewName("redirect:/admFindAllUser");
        return modelAndView;
    }
}
